# -*- coding: utf-8 -*-
"""
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from resource_management import *
from resource_management.core.resources.system import Execute
from resource_management.libraries.functions.check_process_status import check_process_status
from resource_management.libraries.script.script import Script
from resource_management.core.logger import Logger
from kibana_env import kibana_env
import time

class Kibana(Script):
    def install(self, env):
        import params
        env.set_params(params)

        # 删除现的软连接
        Execute('rm -rf /usr/hdp/current/kibana')

        Execute('rm -rf {0}/kibana*'.format(params.hdp_base_dir))

        # Download kibana.tar.gz
        Execute('wget {0} -O kibana.tar.gz'.format(params.kibana_download_url))

        # Install Kibana
        Execute('tar zxvf kibana.tar.gz -C {0} && rm -rf kibana.tar.gz'.format(params.hdp_base_dir))

        Execute('mv {0}/kibana* {1}'.format(params.hdp_base_dir, params.kibana_home))

        Execute('ln -s {0} /usr/hdp/current/kibana'.format(params.kibana_home))

        # Remove Kibana installation files
        Execute('rm -rf kibana.tar.gz')

        Logger.info("Install complete")

    def stop(self, env):
        import params
        env.set_params(params)
        # Stop Kibana
        Execute('ps -ef | grep kibana/bin | grep -v grep |awk \'{print $2}\' | xargs kill -9',
                user=params.kibana_user,
                ignore_failures=True
                )

        # 删除pid文件
        File([params.kibana_pid_file],
             action="delete")

        Logger.info("Stop compelete!")
        time.sleep(5)

    def start(self, env):
        import params
        env.set_params(params)
        # Configure Kibana
        self.configure(env)

        # Start Kibana
        # -p 创建pid文件
        cmd = format("nohup {kibana_home}/bin/kibana -l {kibana_log_file} &")
        Execute(cmd, user=params.kibana_user)

        # 获取kibana的pid并写入文件
        Execute(
            'ps -ef | grep kibana/bin | grep -v grep | awk \'{print $2}\' > ' + params.kibana_pid_file,
            user=params.kibana_user
        )

    def status(self, env):
        import status_params
        env.set_params(status_params)
        check_process_status(status_params.kibana_pid_file)

    def configure(self, env):
        import params
        params.pika_slave = True
        env.set_params(params)
        kibana_env()

if __name__ == "__main__":
    Kibana().execute()



